package com.ruoyi.dataMain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.dataMain.domain.bo.DataConsumptionOriginalBo;
import com.ruoyi.dataMain.domain.vo.DataConsumptionOriginalVo;
import com.ruoyi.dataMain.domain.DataConsumptionOriginal;
import com.ruoyi.dataMain.mapper.DataConsumptionOriginalMapper;
import com.ruoyi.dataMain.service.IDataConsumptionOriginalService;

import java.util.*;

/**
 * 消耗原始Service业务层处理
 *
 * @author linziheng
 * @date 2024-04-17
 */
@RequiredArgsConstructor
@Service
public class DataConsumptionOriginalServiceImpl implements IDataConsumptionOriginalService {

    private final DataConsumptionOriginalMapper baseMapper;


    @Override
    public R secondary(){
        List<DataConsumptionOriginal> list = baseMapper.secondary();
        List<DataConsumptionOriginal> newlist = new ArrayList<>();
        list.forEach(item -> {
            if(!item.getSignatory().equals(item.getSignatoryold())){
                newlist.add(item);
            }
        });
        if(newlist.size()!=0){
            baseMapper.updateBatchById(newlist);
        }


        return R.ok();
    }

    /**
     * 查询消耗原始
     */
    @Override
    public DataConsumptionOriginalVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询消耗原始列表
     */
    @Override
    public TableDataInfo<DataConsumptionOriginalVo> queryPageList(DataConsumptionOriginalBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DataConsumptionOriginal> lqw = buildQueryWrapper(bo);
        Page<DataConsumptionOriginalVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询消耗原始列表
     */
    @Override
    public List<DataConsumptionOriginalVo> queryList(DataConsumptionOriginalBo bo) {
        LambdaQueryWrapper<DataConsumptionOriginal> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DataConsumptionOriginal> buildQueryWrapper(DataConsumptionOriginalBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DataConsumptionOriginal> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDataTime() != null, DataConsumptionOriginal::getDataTime, bo.getDataTime());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountId()), DataConsumptionOriginal::getAccountId, bo.getAccountId());
        lqw.like(StringUtils.isNotBlank(bo.getAccountName()), DataConsumptionOriginal::getAccountName, bo.getAccountName());
        lqw.eq(bo.getTotalConsumption() != null, DataConsumptionOriginal::getTotalConsumption, bo.getTotalConsumption());
        lqw.eq(bo.getNoConsumption() != null, DataConsumptionOriginal::getNoConsumption, bo.getNoConsumption());
        lqw.eq(bo.getConsumption() != null, DataConsumptionOriginal::getConsumption, bo.getConsumption());
        lqw.eq(StringUtils.isNotBlank(bo.getPath()), DataConsumptionOriginal::getPath, bo.getPath());
        return lqw;
    }

    /**
     * 新增消耗原始
     */
    @Override
    public Boolean insertByBo(DataConsumptionOriginalBo bo) {
        DataConsumptionOriginal add = BeanUtil.toBean(bo, DataConsumptionOriginal.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改消耗原始
     */
    @Override
    public Boolean updateByBo(DataConsumptionOriginalBo bo) {
        DataConsumptionOriginal update = BeanUtil.toBean(bo, DataConsumptionOriginal.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DataConsumptionOriginal entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除消耗原始
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
